﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
namespace Project01_DBSecurity
{
    public partial class DAC_RBAC : Form
    {
        public DAC_RBAC()
        {
            InitializeComponent();
        }

      
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btn_grant_dac_Click(object sender, EventArgs e)
        {
            string pri = cb_grant_option.Text;
            string table = cb_table.Text;
            string user = cb_grant_user.Text;
            string option = "";
            if (checkb_grant.Checked)
            {
                option = "with grant option";
            }
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;
              
                comm.CommandText = "GRANT " + pri + " on " + table + " to " + user + "  " + option;
         
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btn_revoke_dac_Click(object sender, EventArgs e)
        {
            string pri = cb_grant_option.Text;
            string table = cb_table.Text;
            string user = cb_revoke_user.Text;
  
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "Revoke " + pri +  " on " + table + " from " + user ;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btn_check_Click(object sender, EventArgs e)
        {
            string table = cb_keyword.Text;
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;
                comm.CommandText = "select ROWNUM, GRANTEE,OWNER,TABLE_NAME,GRANTOR ,PRIVILEGE,GRANTABLE from user_tab_privs where TABLE_NAME = '" + table + "'";
                DataSet privi = new DataSet();
                OracleDataAdapter data = new OracleDataAdapter(comm);
                data.Fill(privi);
                ShowMetadata showPri = new ShowMetadata();
                dtg_check_table.DataSource = privi.Tables[0];
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cb_grant_user_SelectedIndexChanged(object sender, EventArgs e)
        {
          /*  string tab = cb_table.Text;
            OracleCommand comm = new OracleCommand();
            comm.Connection = orcl.conn;

            comm.CommandText = "select * from user_col_comments where table_name= " + tab;
            DataSet table = new DataSet();
            OracleDataAdapter data = new OracleDataAdapter(comm);
            data.Fill(table);
          */
        }

        private void bt_grant_role_Click(object sender, EventArgs e)
        {
            string pri = cb_grant_role_pri.Text;
            string table = cb_table_grant_role.Text;
            string role = cb_role_grant_dac.Text;


            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "grant " + pri + " on " + table + " to " + role;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btn_revoke_role_Click(object sender, EventArgs e)
        {

            string pri = cb_revole_role_pri.Text;
            string table = cb_table_revoke_role.Text;
            string role = cb_role_revoke.Text;


            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "revoke " + pri + " on " + table + " from " + role;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_grant_RBAC_Click(object sender, EventArgs e)
        {
            string role = cb_grant_RBAC.Text;
            string  user =cb_user_grant_RBAC .Text;
            string option = "";
            if (ck_grant_option_RBAC.Checked)
            {
                option = "with admin option";
            }
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "grant " + role + " to " + user + " " + option;
              
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_revoke_RBAC_Click(object sender, EventArgs e)
        {
            string role = cb_revoke_RBAC.Text;
            string user = cb_user_revoke_RBAC.Text;
           
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "revoke " + role + " from " + user ;
              
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_check_role_Click(object sender, EventArgs e)
        {
            string role = cb_role_check.Text;
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;
             
                comm.CommandText = "select ROWNUM, USERNAME,GRANTED_ROLE,ADMIN_OPTION,DEFAULT_ROLE from user_role_privs where GRANTED_ROLE = '" + role + "'";
                DataSet privi = new DataSet();
                OracleDataAdapter data = new OracleDataAdapter(comm);
                data.Fill(privi);
                ShowMetadata showPri = new ShowMetadata();
                dgw_check_role.DataSource = privi.Tables[0];
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_ok_grant_proc_Click(object sender, EventArgs e)
        {

            string proc_name = cb_grant_proc.Text;
            string user = cb_grant_user_proc.Text;
            string option = "";
            if (check_grant_proc.Checked)
            {
                option = "with grant option";
            }
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "Grant execute on " + proc_name + " to " +  user + " " + option ;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_ok_revoke_Proc_Click(object sender, EventArgs e)
        {
            string proc_name = cb_revoke_proc.Text;
            string user = cb_revoke_user_proc.Text;
            
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "Revoke execute on " + proc_name + " from " + user ;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_ok_grant_function_Click(object sender, EventArgs e)
        {
            string func_name = cb_grant_function.Text;
            string user = cb_user_grant_function.Text;
            string option = "";
            if (check_grant_function.Checked)
            {
                option = "with grant option";
            }
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "Grant execute on " + func_name + " to " + user + " " + option;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_revoke_function_Click(object sender, EventArgs e)
        {

            string func_name = cb_revoke_function.Text;
            string user = cb_user_revoke_function.Text;

            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "Revoke execute on " + func_name + " from " + user;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_grant_view_Click(object sender, EventArgs e)
        {
            string view_name = cb_grant_view.Text;
            string user = cb_user_grant_view.Text;
            string option = "";
            if (check_grant_view.Checked)
            {
                option = "with grant option";
            }
            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "Grant select on " + view_name + " to " + user + " " + option;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_revoke_view_Click(object sender, EventArgs e)
        {
            string view_name = cb_revoke_view.Text;
            string user = cb_user_revoke_view.Text;

            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;

                comm.CommandText = "Revoke select on " + view_name + " from " + user;
                comm.ExecuteNonQuery();
                MessageBox.Show("Succeed!", "Anncouncement", MessageBoxButtons.OK, MessageBoxIcon.Information);
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_check_priv_proc_Click(object sender, EventArgs e)
        {
            string proc = cb_check_priv_proc.Text;
            
            try{
             OracleCommand comm = new OracleCommand();
             comm.Connection = orcl.conn;
            comm.CommandText = "select ROWNUM , GRANTEE ,OWNER, TABLE_NAME,GRANTOR,PRIVILEGE,GRANTABLE from user_tab_privs where TABLE_NAME = '"+ proc +"'";
            DataSet proc_pri = new DataSet();

            OracleDataAdapter data = new OracleDataAdapter(comm);
            data.Fill(proc_pri);
            dtg_check_proc.DataSource = proc_pri.Tables[0];
           
            comm.Dispose();
             }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btn_check_priv_view_Click(object sender, EventArgs e)
        {
            string view = cb_check_priv_view.Text;

            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;
                comm.CommandText = "select ROWNUM , GRANTEE ,OWNER, TABLE_NAME,GRANTOR,PRIVILEGE,GRANTABLE from user_tab_privs where TABLE_NAME = '" + view + "'";
                DataSet view_pri = new DataSet();

                OracleDataAdapter data = new OracleDataAdapter(comm);
                data.Fill(view_pri);
                dgw_check_priv_view.DataSource = view_pri.Tables[0];
              
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btn_check_priv_func_Click(object sender, EventArgs e)
        {
            string  function = cb_check_pri_function.Text;

            try
            {
                OracleCommand comm = new OracleCommand();
                comm.Connection = orcl.conn;
                comm.CommandText = "select ROWNUM , GRANTEE ,OWNER, TABLE_NAME,GRANTOR,PRIVILEGE,GRANTABLE from user_tab_privs where TABLE_NAME = '" + function + "'";
                DataSet func_pri = new DataSet();

                OracleDataAdapter data = new OracleDataAdapter(comm);
                data.Fill(func_pri);
                dtw_check_pri_func.DataSource = func_pri.Tables[0];
                
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
